package com.design.template;

import cn.hutool.core.util.ObjectUtil;
import com.design.strategy.StrategyService;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略模式上下文
 *
 * @author Zee
 * @date 2020年4月30日
 * @since v1.0
 */
@Component
public class TemplateContext {
    private final ConcurrentHashMap<String, AbstractTemplate> strategyMap = new ConcurrentHashMap<>();

    private AbstractTemplate getTemplate(String type) {
        return strategyMap.get(type);
    }

    public void setTemplate(String type, AbstractTemplate template) {
        strategyMap.put(type, template);
    }

    /**
     * 执行模板方法
     *
     * @param type
     */
    public void execute(String type) {
        AbstractTemplate template = getTemplate(type);
        if (ObjectUtil.isNotNull(template)) {
            template.execute();
        }

    }
}
